let state = {

	msg: 'cart vuex  js',
	list: [

		{
			checked: false,
			id: 11,
			title: "商品标题111",
			cover: "/static/images/demo/list/1.jpg",
			// 选中商品属性
			attrs: [{
					title: "颜色",
					selected: 0,
					list: [{
							name: '火焰红',
						},
						{
							name: '炭黑',
						},
						{
							name: '冰川兰',
						}
					]
				},
				{
					title: "容量",
					selected: 0,
					list: [{
							name: '64GB',
						},
						{
							name: '128GB',
						},
					]
				},
				{
					title: "套餐",
					selected: 0,
					list: [{
							name: '标配',
						},
						{
							name: '套餐一',
						},
						{
							name: '套餐二',
						}
					]
				},
			],
			pprice: 336,
			num: 1,
			minnum: 1,
			maxnum: 10, // 该商品最大商品数，跟库存有关
		},
		{
			checked: false,
			id: 12,
			title: "商品标题111",
			cover: "/static/images/demo/list/1.jpg",
			// 选中商品属性
			attrs: [{
					title: "颜色",
					selected: 0,
					list: [{
							name: '火焰红',
						},
						{
							name: '炭黑',
						},
						{
							name: '冰川兰',
						}
					]
				},
				{
					title: "容量",
					selected: 0,
					list: [{
							name: '64GB',
						},
						{
							name: '128GB',
						},
					]
				},
				{
					title: "套餐",
					selected: 0,
					list: [{
							name: '标配',
						},
						{
							name: '套餐一',
						},
						{
							name: '套餐二',
						}
					]
				},
			],
			pprice: 336,
			num: 1,
			minnum: 1,
			maxnum: 10, // 该商品最大商品数，跟库存有关
		},
		{
			checked: false,
			id: 13,
			title: "商品标题3333",
			cover: "/static/images/demo/list/1.jpg",
			// 选中商品属性
			attrs: [{
					title: "颜色",
					selected: 0,
					list: [{
							name: '火焰红',
						},
						{
							name: '少女粉',
						},
						{
							name: '炭黑',
						},
						{
							name: '冰川兰',
						}

					]
				},
				{
					title: "容量",
					selected: 0,
					list: [{
							name: '64GB',
						},
						{
							name: '128GB',
						},
					]
				},
				{
					title: "套餐",
					selected: 0,
					list: [{
							name: '顶级配置',
						},
						{
							name: '套餐一',
						},
						{
							name: '套餐二',
						}
					]
				},
			],
			pprice: 336,
			num: 1,
			minnum: 1,
			maxnum: 10, // 该商品最大商品数，跟库存有关
		},
	],
	// 选中列表（存放选中的id）
	selectedList: [],

	//显示属性框
	popupShow: "none",

	initIndex: -1,
	// arrData:[]
}



// 全选 单选    总价格计算(选中的) 



// 添加 
// 删除 
// 弹框属性选择
// 数量的加减  


let mutations = {

	//全选功能实现   只能实现全选  不能实现 全不选

	//全选方法 
	selectAll(state) {
		state.selectedList = state.list.map((v, index) => {
			v.checked = true
			return v.id
		})

	},


	// 全不选方法
	unSelectAll(state) {
		state.list.map((v, index) => {
			v.checked = false

		})
		state.selectedList = []


	},


	//删除选中商品  {}
	delGoods(state, newItems) {


		state.list = newItems


	},

	//单个商品选中
	selectItemMutations(state, index) {
		// alert('selectItemMutations')
		// alert(index)
		// currentItem.checked=!currentItem.checked

		/*
			  // bug 代码
		let   currentItem= state.list[index]
		
		if(!currentItem.checked) {
         state.	selectedList.push(currentItem.id)	
			
		}else {
			alert('else')
			currentItem.checked=false
			 state.	selectedList.splice(index,1)
		}
		*/


		//通过索引 去判断    通过id 去判断

		let currentItem = state.list[index]
		let id = currentItem.id

		// 判断 全选数组中有没有这个id  如果有表示之前已经被选中了 那么就让当前的单选框不被选中  selectedList 里面的数量为false 那个值要删除   
		// 如果没有这个id  表示之前没有没选中 那么就让当前的单选框选中
		// let flag=state.selectedList.indexOf(id) >-1;
		let i = state.selectedList.indexOf(id)

		if (i > -1) { //表示在


			currentItem.checked = false;
			state.selectedList.splice(i, 1)
			return

		}
		//不在的情况 

		currentItem.checked = true;
		state.selectedList.push(id)


	},



	//初始化状态
	initPopupIndex(state, index) {



		state.initIndex = index //知道是点击的哪一个商品


	},

	//属性隐藏
	// HidePopupInit(state) {

	// 	state.initIndex=-1; //   恢复初始化状态	
	// },

	//真正将商品加入到购物车 更新state的数据
	addGoodsToCart(state, goods) {
		state.list.unshift(goods)
	}



}




let actions = {

	// 异步调用全选与反选
	doSelectAll({
		commit,
		getters
	}) {

		// 判断条件getters。checkedAll 是否为true 跟false 
		getters.checkedAll ? commit('unSelectAll') : commit('selectAll')
	},

	// 当个商品选中
	selectItem({
		commit
	}, index) {
		commit('selectItemMutations', index)
	},









	//删除商品
	doDelGoods({
		commit
	}, item) {

		uni.showModal({
			content: '是否需要删除该商品',
			success: (res) => {
				if (res.confirm) {
					let newItmes = state.list.filter(v => {

						let i = state.selectedList.indexOf(v.id);

						if (i == -1) {
							return true
						} else {
							state.selectedList.splice(i, 1)
						}

						// i===-1?   return  true:state.selectedList.splice(i,1)
						// i!=-1?state.selectedList.splice(i,1):true  
					})
					console.log(newItmes)
					commit('delGoods', newItmes)
					uni.showToast({
						title: "删除成功"
					})

				}




			}
		})



	},


	// 显示商品属性弹出框
	doShowPopup({
		state,
		commit
	}, index) {



		state.popupShow = 'show'


		commit('initPopupIndex', index)
	},

	// 隐藏商品属性弹出框
	doHidePopup({
		state,
		commit
	}) {

		state.popupShow = 'none'


		commit('initPopupIndex', -1)






	}







}

let getters = {

	// 全选    

	checkedAll(state) {
		// return false 
		if (state.list.length) {
			return state.selectedList.length == state.list.length

		}
		return false

	},


	//商品总价格计算  
	totalPrice(state) {
		let total = 0;
		state.list.map(v => {
			//判断选中的商品  进行 单价*数量    累加 

			// if (v.checked) {
			// 	total += v.pprice * v.num
			// }
			if(state.selectedList.indexOf(v.id)>-1) {
				total+=v.pprice*v.num
			}
		})

		return total

	},

	// 如果一个商品都没有禁用全选按钮点击
	disableSelectAll(state) {
		return state.list.length === 0
	},


	// 拿到当前需要修改属性的商品
	popupData: (state) => {
		return state.initIndex > -1 ? state.list[state.initIndex] : {}
	}














}






export default {

	state,
	mutations,
	actions,
	getters



}
